package org.jash.learnmvp.news;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.scwang.smart.refresh.layout.SmartRefreshLayout;

import org.jash.learnmvp.R;
import org.jash.learnmvp.entity.NewsPageEntity;
import org.jash.learnmvp.mvp.BaseFragment;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link CategoryFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class CategoryFragment extends BaseFragment<ConStruct.ICategoryView, CategoryPresenter> implements ConStruct.ICategoryView {


    private static final String ARG_ID = "id";
    private static final String ARG_NAME = "name";


    private int id;
    private String name;
    private RecyclerView recycler;
    private SmartRefreshLayout refresh;
    private NewsAdapter adapter = new NewsAdapter();
    private int size;
    private int page;

    public static CategoryFragment newInstance(int id, String name) {
        CategoryFragment fragment = new CategoryFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_ID, id);
        args.putString(ARG_NAME, name);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            id = getArguments().getInt(ARG_ID);
            name = getArguments().getString(ARG_NAME);
        }
    }


    @Override
    protected int layoutId() {
        return R.layout.fragment_category;
    }

    @Override
    protected void initData() {
        presenter = new CategoryPresenter();
        size = 20;
        page = 1;
        presenter.loadNewsByCategory(id, page, size);
    }

    @Override
    protected void initView() {
        recycler = getView().findViewById(R.id.recycler);
        refresh = getView().findViewById(R.id.refresh);
        recycler.setAdapter(adapter);
        adapter.setListener(bean -> {
            requireContext().startActivity(new Intent(requireContext(), NewsDetailActivity.class) {{
                putExtra("id", bean.getId());
            }});
        });
        refresh.setOnRefreshListener(v -> {
            page = 1;
            presenter.loadNewsByCategory(id, page, size);
        });
        refresh.setOnLoadMoreListener(v -> {
            presenter.loadNewsByCategory(id, ++page, size);
        });
    }

    @Override
    public void newsSuccess(NewsPageEntity.DataBean page) {
        if (this.page == 1) {
            adapter.clear();
        }
        adapter.addAll(page.getRecords());
        refresh.closeHeaderOrFooter();
    }

    @Override
    public void failure(Throwable t) {
        Toast.makeText(requireContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
        Log.e("TAG", "failure: ", t);
        refresh.closeHeaderOrFooter();
    }
}